<?php
if (!defined('__COLA_DIR__')) exit;

/**
 * Widget_Themes_Ucenter  
 * 
 * @author edikud
 * @package Widget
 * @copyright Copyright (c) 2019 Cola (http://www.mcooo.com)
 * @license GNU General Public License 2.0
 */
class Widget_Themes_Ucenter extends Widget_Abstract_Options
{
    /**
     * 执行函数
     *
     * @access public
     * @return void
     */
    public function execute()
    {
		$this->user->pass('admin');
    }
	
    public function themesList()
    {
        $this->widget('Widget_Scripts')->set("
            $(document).ready(function () {
			    $(\".cola-content\").ajaxClick('.activate');
		    });");
		
		$this->widget('Widget_Themes_List')->to($themes);

		$Layout = new Layout();
		$Layout->addClass('cola-themes');
			$Layout->header(array(
			    _t('截图'),
				_t('详情'),
			));
			while ($themes->next()){
				
                $parseStr = 
				'<div class="theme-title">'.('' != $themes->title ? $themes->title : $themes->name).'</div>
                <div class="cite">';
                if($themes->author){
					$parseStr .= _t('作者').': ';
					if($themes->homepage){
						$parseStr .= '<a href="'.$themes->homepage.'">' . $themes->author . '</a>';
					} else {
					    $parseStr .= $themes->author;
					}					
					$parseStr .= '&nbsp;&nbsp;';
				}
                if($themes->version){
					$parseStr .= _t('版本').': '.$themes->version;
				}
                $parseStr .= '</div>
                <p>'.nl2br($themes->description).'</p>';
                $parseStr .= '<p><a class="cola-button text-md" href="'.$this->options->ucenterUrl('theme?theme=' . $themes->name, false).'">'._t('编辑').'</a> &nbsp;';
                if($this->options->theme != $themes->name){
                    $parseStr .= '<a class="cola-button activate text-md" href="'.$this->security->actionUrl('ajax?changeTheme', false).'" data-theme="'. $themes->name .'" data-loading-text="loading..">'._t('启用').'</a>';
				}
                if ($this->options->theme == $themes->name && Widget_Themes_Config::isExists()){
                    $parseStr .= '<a class="cola-button text-md" href="'.$this->options->ucenterUrl('options-theme', false).'">'._t('设置').'</a>';
                }
				$parseStr .= '</p>';

			    $Layout->lists(array(
				
				    '<img src="'.$themes->screen.'" alt="'.$themes->name.'" />',
					$parseStr
					), array('id'=>'theme-'. $themes->name, 'style'=>$themes->activated ?'background-color: #fff9e8;': ''));
			}
			$parseStr = $Layout->render();

		return $parseStr;
	}
	
    public function themesEditor()
    {
		
		$this->widget('Widget_Themes_Files')->to($files);
		
		$html = '
		<div class="cola-theme-edit">
			<h3>'._t('模板文件').'</h3>
			<div class="cola-theme-files">';
                while($files->next()){
                $html .= '
				<a class="cola-themeFile'.($files->current? '  current' : '').'" href="'.$this->options->ucenterUrl('theme?theme=' . $files->currentTheme() . '&file=' . $files->file, 0).'">'.$files->file.'</a>';
                }
            $html .= '
			</div>';

		$textarea = '
        <form method="post" name="theme" action="'.$this->security->actionUrl('themes-edit', 0).'">
	        <textarea class="form-control" name="content" '.(!$files->currentIsWriteable()? 'disabled="disabled"' : '').'>'.$files->currentContent().'</textarea>
            <p>';
            if($files->currentIsWriteable()){
                $textarea .= '
				<input type="hidden" name="theme" value="'.$files->currentTheme().'" />
                <input type="hidden" name="edit" value="'.$files->currentFile().'" />';
			    $textarea .= (new Form_Submit())->value(_t('保存文件'));
            } else {
				$textarea .= (new Form_Submit())->value(_t('保存文件'))->input->setAttr('disabled', 'disabled');
                $textarea .= '<em class="text-danger">'._t('此文件无法写入').'</em>';
            }
        $textarea .= '
			</p>
		</form>';
		
		/** 构建表格 */
        $form = new Form($this->security->actionUrl('themes-edit', false));
	    $form->set($textarea);
	
        $html .= $form->render() . '</div>';

		return $html;
	}

}

